package io.codegeneratetool.util.validate;

import io.codegeneratetool.util.PackageUtil;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
 

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

/** 验证注解解析工具类
 * 说明：
 */
public  class ValidateAnnotationUtil {
	
		
		/**  验证信息 容器 */
		protected Map<String, Object> validateContainer = new HashMap<String, Object>();
				
		/** 验证信息文本  */
		protected Map<String, Object> messages = new HashMap<String, Object>();
	
	
	
	
		/**
		 * 说明：
		 * @author jiaxf 创建时间2012-10-24下午5:33:32
		 * @param 
		 */
		public <T> String process(T t , Field field){
			BaseProcessorChain<?> chain =  constructChain();
			if(chain==null){
				return "";
			}
			chain.doProcess(t, field, validateContainer, messages);
			if(validateContainer.isEmpty()){
				return "";
			}
			if(!messages.isEmpty()){
				validateContainer.put("messages", messages);
			}
			ObjectMapper mapper = new ObjectMapper();
			try {
			String writeValueAsString = mapper.writeValueAsString(validateContainer);
			writeValueAsString = writeValueAsString.replaceAll("\"","'");
			System.out.println("验证处理 "+ writeValueAsString);
			return 	writeValueAsString ;
			} catch (JsonGenerationException e) {
			 
				e.printStackTrace();
			} catch (JsonMappingException e) {
			 
				e.printStackTrace();
			} catch (IOException e) {
				 
				e.printStackTrace();
			}
			return "";
		}
		
		
		/**
		 * 说明： 构造chain , 里面的类型手工添加  ,职能在 kjlink.wuxi.core.reflect.validate 包内
		 * 组装的责任链模式,详见 BaseProcessorChain.class
		 * @author jiaxf 创建时间2012-10-24下午5:58:51
		 * @param 
		 */
		private BaseProcessorChain<?>  constructChain(){
			 Set<Class<?>> classes = PackageUtil.getClasses(this.getClass().getPackage().getName());
			 BaseProcessorChain<?> chain= null; 
			 BaseProcessorChain<?> chainThis=null ;
			 for (Class<?> class1 : classes) {
				if(class1.getSuperclass()==BaseProcessorChain.class){
					if(chain==null){
						try {
							chain = (BaseProcessorChain<?>) class1.newInstance();
							chainThis = chain;
						} catch (Exception e) {
							 e.printStackTrace();
						}
						
					}else{
						try {
							BaseProcessorChain<?> newInstance = (BaseProcessorChain<?>) class1.newInstance();
							chainThis.setNextChain( newInstance);
							chainThis = newInstance;
						} catch (InstantiationException e) {
							 
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							 
							e.printStackTrace();
						}
					}
					
					
				}
			}
			
			
			
			
			return chain;
		}
		
		
		public static void main(String[] args) {
//			ValidateAnnotationUtil annotationUtil = new ValidateAnnotationUtil();
//			SampleBean sb = new SampleBean();
//			Field field = SampleBean.class.getDeclaredFields()[0];
//			annotationUtil.process(sb, field);
		}
		
}
